package com.jessechanrui.template.thread;

/**
 * 线程其他操作
 *
 * @author chenrui
 * @date 2024/12/12
 */
public class ThreadJoinOrInterruptCase {
    public static void main(String[] args) {

        Thread threadA = new Thread(new ThreadA(), "线程A");
        Thread threadB = new Thread(new ThreadB(), "线程B");
        threadA.start();
        try {
            Thread.sleep(100);
//            threadA.interrupt();
            threadA.join(100);
            threadB.start();
            threadB.join();
            System.out.println("主线程执行完成");

        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }


    }
    
    static class ThreadA implements Runnable{
        @Override
        public void run() {
            try {
                System.out.println(Thread.currentThread().getName() + " 执行开始");
                Thread.sleep(500);
                System.out.println(Thread.currentThread().getName() + " 执行结束");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    static class ThreadB implements Runnable{
        @Override
        public void run() {
            try {
                System.out.println(Thread.currentThread().getName() + " 执行开始");
                Thread.sleep(50000);
                System.out.println(Thread.currentThread().getName() + " 执行结束");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
